package com.vidmt.api.modules.sys.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vidmt.api.common.pojo.ApiResultServer;
import com.vidmt.api.common.utils.DateUtil;
import com.vidmt.api.modules.sys.Enums.PayType;
import com.vidmt.api.modules.sys.entity.Order;
import com.vidmt.api.modules.sys.service.OrderService;
import com.vidmt.api.modules.sys.utils.UserStatUtil;
import com.vidmt.api.modules.sys.utils.VerStatUtil;
import com.vidmt.api.modules.sys.utils.VerStatUtil.LastLoginItem;

@Controller
@RequestMapping("${adminPath}/web/statistics")
public class WebStatController {
	private static final Logger log = LoggerFactory.getLogger(WebStatController.class);

	@Autowired
	private OrderService orderService;

	@RequestMapping("index.html")
	public String index(){
		return "modules/sys/statistics";
	}
	
	@ResponseBody
	@RequestMapping("/last10info.*")
	public ApiResultServer last10info() {
		return new ApiResultServer(UserStatUtil.getLast10Stat());
	}

	@ResponseBody
	@RequestMapping("/hourinfo.*")
	public ApiResultServer hourinfo() {
		return new ApiResultServer(UserStatUtil.getHourinfo());
	}

	@ResponseBody
	@RequestMapping("/verinfo.*")
	public ApiResultServer verinfo() {
		Map<String, LastLoginItem> map = VerStatUtil.getLastLoginMap();
		return new ApiResultServer(map);
	}

	private static class ADayInfo {
		public int alimoney;
		public int alicnt;
		public int wxmoney;
		public int wxcnt;
		public int iapmoney;
		public int iapcnt;

		public int iyearlvlcnt;
		public int ayearlvlcnt;
		public int realmoney;
	}

	@ResponseBody
	@RequestMapping("/moneyinfo.*")
	public ApiResultServer moneyinfo() {
		final int DAY_LEN=15;
		ADayInfo[] dayinfos = new ADayInfo[14];
		for (int i = 0; i < dayinfos.length; i++) {
			dayinfos[i] = new ADayInfo();
		}

		Calendar cld = Calendar.getInstance();
		Date now = cld.getTime();
		long aday = 24L * 60 * 60 * 1000;
		Date todayStart = DateUtil.getDateStart(now);
		long todayEnd = todayStart.getTime() + aday - 1;

		List<Order> orderlist = orderService.findLatest(new Date(todayEnd - DAY_LEN * aday));

		for (Order order : orderlist) {
			long logtime = order.getPayTime().getTime();
			int days = (int) ((todayEnd - logtime) / aday);

			String[] skus = order.getSkus();
			for (String sku : skus) {
				if (sku.equals("YEAR")) {
					if (order.getPayType() == PayType.IAP) {
						dayinfos[days].iyearlvlcnt += 1;
					} else {
						dayinfos[days].ayearlvlcnt += 1;
					}
					break;
				}
			}
			if (days < 15) {
				int totalfee = order.getTotalFee();
				dayinfos[days].realmoney += order.getRealFee();
				switch (order.getPayType()) {
				case ALI:
					dayinfos[days].alimoney += totalfee;
					dayinfos[days].alicnt += 1;
					break;
				case WX:
					dayinfos[days].wxmoney += totalfee;
					dayinfos[days].wxcnt += 1;
					break;
				case IAP:
					dayinfos[days].iapmoney += totalfee;
					dayinfos[days].iapcnt += 1;
					break;
				default:
					break;
				}
			}
		}

		ApiResultServer data = new ApiResultServer();
		data.put("days", dayinfos);
		cld.set(Calendar.DAY_OF_MONTH, 1);
		Order orderMonthFee = orderService.findTotalFee(DateUtil.getDateStart(cld.getTime()));
		if (orderMonthFee != null) {
			data.put("monthmoney", orderMonthFee.getTotalFee());
			data.put("monthrealmoney", orderMonthFee.getRealFee());
		}else{
			data.put("monthmoney", 0);
			data.put("monthrealmoney", 0);
		}
	
		

		return data;
	}
}
